﻿Imports Global_Visas_Business_Layer
Imports System.Data
Imports System.ComponentModel
Partial Public Class frmAdminManageCases

    
    Private Sub Btn_Close_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles Btn_Close.Click
        If Me.Btn_Close.Content = "Cancel" Then
            Me.Btn_Update.IsEnabled = False
            Me.Combo_Status.ItemsSource = Nothing
            Me.SQL_CaseStatus()
            Me.Combo_Status.SelectedIndex = -1
            Me.Combo_Status.IsEnabled = False

            Me.Lbl_Details.Visibility = Windows.Visibility.Hidden
            Me.Lbl_Headings.Visibility = Windows.Visibility.Hidden

            Me.Btn_Lookup.IsEnabled = True
            Me.Txt_CaseID.IsReadOnly = False
            Me.Txt_CaseID.Text = String.Empty
            Me.NewStatus_Label.IsEnabled = False
            Me.Btn_Close.Content = "Close"

            Me.Txt_CaseID.Focus()
        Else

            Me.Close()
        End If
    End Sub
    Private Sub SQL_CaseStatus()
        Dim StrSQL As String
        StrSQL = "select CaseStage_ID,CaseStage_Number,CaseStage_Title from L_casestages"
        Dim caseStagesList As DataTable = CommonDataFunctions.GetSQLDatatable(StrSQL)
        With Me.Combo_Status
            .ItemsSource = CType(caseStagesList, IListSource).GetList
            .SelectedValuePath = "CaseStage_Number"
            .DisplayMemberPath = "CaseStage_Title"
        End With
    End Sub
    Private Sub Btn_Lookup_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles Btn_Lookup.Click
        Try
            Dim StrSQL As String, StrMess As String, Rst As DataTable, LngID As Long, IntStatus As Integer, IntDiagram As Integer, IntFinance As Integer

            If Me.Txt_CaseID.Text = "" Then
                Call MsgBox("Please enter the Case ID you want to change", , "Error")
                Me.Txt_CaseID.Focus()
                Exit Sub
            End If

            If Not IsNumeric(Me.Txt_CaseID.Text) Then
                Call MsgBox("Not a valid Case ID", , "Error")
                Me.Txt_CaseID.Focus()
                Exit Sub
            End If

            LngID = Me.Txt_CaseID.Text

            StrSQL = "SELECT D_Case.Case_DTStatus, D_Case.Case_StatusID, isnull(D_Company.Company_Name,'Private') as Company, D_Person.Person_FName + ' ' + D_Person.Person_LName as Candidate, L_Products.Product_Name, L_Consultant.Con_Name, CaseStage_Title"
            StrSQL = StrSQL & " FROM D_Case LEFT OUTER JOIN"
            StrSQL = StrSQL & " L_Consultant ON D_Case.Case_ConCWID = L_Consultant.Con_ID LEFT OUTER JOIN"
            StrSQL = StrSQL & " L_Products ON D_Case.Case_ProductID = L_Products.Product_ID LEFT OUTER JOIN"
            StrSQL = StrSQL & " D_Person ON D_Case.Case_CandidateID = D_Person.Person_ID LEFT OUTER JOIN"
            StrSQL = StrSQL & " D_Company ON D_Case.Case_CompanyID = D_Company.Company_ID LEFT OUTER JOIN"
            StrSQL = StrSQL & " L_CaseStages ON D_Case.Case_StatusID = L_CaseStages.CaseStage_Number"
            StrSQL = StrSQL & " WHERE D_Case.Case_ID = " & LngID

            Rst = CommonDataFunctions.GetSQLDatatable(StrSQL)

            If Rst.Rows.Count = 0 Then
                Rst.Dispose()
                Call MsgBox("Case ID " & LngID & " not found!", , "Error")
                Exit Sub
            End If

            StrMess = "Product:" & vbCrLf
            StrMess = StrMess & "Candidate:" & vbCrLf
            StrMess = StrMess & "Company:" & vbCrLf
            StrMess = StrMess & "Caseworker:" & vbCrLf
            StrMess = StrMess & "Current Status:" & vbCrLf
            StrMess = StrMess & "Date Set:" & vbCrLf

            Me.Lbl_Headings.Content = StrMess
            Me.Lbl_Headings.Visibility = Windows.Visibility.Visible

            StrMess = Rst.Rows(0)("Product_Name") & vbCrLf
            StrMess = StrMess & Rst.Rows(0)("Candidate") & vbCrLf
            StrMess = StrMess & Rst.Rows(0)("Company") & vbCrLf
            StrMess = StrMess & Rst.Rows(0)("Con_Name") & vbCrLf
            StrMess = StrMess & Rst.Rows(0)("Casestage_Title") & vbCrLf
            StrMess = StrMess & Rst.Rows(0)("Case_DTStatus")

            IntStatus = Rst.Rows(0)("Case_StatusID")

            Rst.Dispose()

            Me.Lbl_Details.Content = StrMess
            Me.Lbl_Details.Visibility = Windows.Visibility.Visible

            IntFinance = 0

            If IntStatus > 99 Then
                'This case has been sold, check to see if finance has been raised yet
                StrSQL = "Select Payment_ID from A_Payments where Payment_CaseId = " & LngID
                Rst = CommonDataFunctions.GetSQLDatatable(StrSQL)

                If Rst.Rows.Count > 0 Then IntFinance = 1
                Rst.Dispose()

                StrSQL = "Select Invoice_ID from A_Invoices where Invoice_CaseId = " & LngID
                Rst = CommonDataFunctions.GetSQLDatatable(StrSQL)

                If Rst.Rows.Count > 0 Then IntFinance = IntFinance + 2
                Rst.Dispose()

                Select Case IntFinance

                    Case 1
                        Call MsgBox("At least one payment has been lodged for this case, it therefore cannot be moved back to a pre-sale status", , "Warning")

                    Case 2
                        Call MsgBox("At least one invoice has been lodged for this case, it therefore cannot be moved back to a pre-sale status", , "Warning")

                    Case 1
                        Call MsgBox("At least one Payment and Invoice has been created for this case, it therefore cannot be moved back to a pre sale status", , "Warning")

                End Select

            End If

            IntDiagram = GblFunc_Get_Dataset(LngID, 1)

            If IntDiagram = 0 Then IntDiagram = 2

            StrSQL = "SELECT L_CaseStages.CaseStage_Number, MAX(L_CaseStages.CaseStage_Title) AS StageTitle"
            StrSQL = StrSQL & " FROM " 'L_StageLinkDiagram INNER JOIN"
            StrSQL = StrSQL & " L_StageLinks Inner Join " 'ON L_StageLinkDiagram.StLDiagram_StageLinkID = L_StageLinks.StageLink_ID INNER JOIN"
            StrSQL = StrSQL & " L_CaseStages ON L_StageLinks.StageLink_FromID = L_CaseStages.CaseStage_Number"
            StrSQL = StrSQL & " WHERE (L_StageLinks.StageLink_DiagramID = " & IntDiagram & ") And (L_CaseStages.CaseStage_Number < " & IntStatus & ")"

            If IntFinance <> 0 Then
                'Exclude pre sale stages
                StrSQL = StrSQL & " And (L_CaseStages.CaseStage_Number > 99)"

            End If

            StrSQL = StrSQL & " GROUP BY L_CaseStages.CaseStage_Number"
            StrSQL = StrSQL & " ORDER BY L_CaseStages.CaseStage_Number"

            Me.Combo_Status.ItemsSource = Nothing
            Me.FillCombo(StrSQL)
            
            'Call GblFunc_Populate_List(StrSQL, "ChangeStatus_List", Form_FMA_ChangeStatus.Combo_Status)

            Me.Combo_Status.IsEnabled = True
            Me.Btn_Update.IsEnabled = True
            Me.NewStatus_Label.IsEnabled = True
            Me.Label2.IsEnabled = True
            Me.Combo_Status.Focus()
            Me.Txt_CaseID.IsReadOnly = True
            Me.Btn_Lookup.IsEnabled = False

            Me.Btn_Close.Content = "Cancel"

        Catch ex As Exception
            CommonFuncs.ErrorHandler(ex, "frmAdminManageCases-Btn_Lookup_Click")
        End Try
    End Sub
    Private Sub FillCombo(ByVal StrSQL As String)
        Dim dataItems As DataTable = CommonDataFunctions.GetSQLDatatable(StrSQL)
        With Me.Combo_Status
            .ItemsSource = CType(dataItems, IListSource).GetList
            .SelectedValuePath = "CaseStage_Number"
            .DisplayMemberPath = "StageTitle"
            .SelectedIndex = -1
        End With
    End Sub

    Public Function GblFunc_Get_Dataset(ByVal LngCaseID As Long, ByVal IntSet As Integer) As Integer
        

        ' IntSet 1 = Case Stages Diagram
        ' IntSet 2 = Multi Function Button Group
        ' IntSet 3 = Stock Letter Set
        ' IntSet 4 = Charge Type Group
        ' IntSet 5 = Event type group
        ' IntSet 6 = DT Category
        ' IntSet 7 = Calc Set

        Dim Rst As DataTable, StrSQL As String
        Dim IntProductID As Integer, IntProductType As Integer, returnValue As Integer

        StrSQL = "Select Case_ProductId from D_Case where case_Id = " & LngCaseID
        Rst = CommonDataFunctions.GetSQLDatatable(StrSQL)

        If Rst.Rows.Count = 0 Then
            MsgBox("Error state Case not found")
            Exit Function
        End If

        IntProductID = Rst.Rows(0)("Case_ProductID")

        Rst.Dispose()

        If IntProductID <> 0 Then

            'Get the data sets from the product
            StrSQL = "Select Product_Type, Product_StagediagramId, Product_MFBGroupID, Product_SLSID, Product_ChargeGroupID, Product_EventGroupID, Product_DTCategory, Product_CalcSet from L_Products where product_id = " & IntProductID

            Rst = CommonDataFunctions.GetSQLDatatable(StrSQL)

            If Rst.Rows.Count = 0 Then
                MsgBox("Error state Product not found")
                Exit Function
            End If

            IntProductType = Rst.Rows(0)("Product_Type")

            'Set the function to the required value
            Select Case IntSet
                Case 1
                    returnValue = Rst.Rows(0)("Product_StageDiagramID")

                Case 2
                    returnValue = Rst.Rows(0)("Product_MFBGroupID")

                Case 3
                    returnValue = Rst.Rows(0)("Product_SLSID")

                Case 4
                    returnValue = Rst.Rows(0)("Product_ChargeGroupID")

                Case 5
                    returnValue = Rst.Rows(0)("Product_EventGroupID")

                Case 6
                    returnValue = Rst.Rows(0)("Product_DTCategory")

                Case 7
                    returnValue = Rst.Rows(0)("Product_CalcSet")

                Case Else
                    returnValue = 0

            End Select

            Rst.Dispose()

            'Do we have what we want?
            If GblFunc_Get_Dataset = 0 Then
                'No, try again with the product type

                StrSQL = "Select ProductType_StageDiagramID, ProductType_MFBGroupID, ProductType_SLSID, ProductType_ChargeGroupID, ProductType_EventGroupID, ProductType_DTCategory, ProductType_CalcSet "
                StrSQL = StrSQL & " FROM L_ProductTypes "
                StrSQL = StrSQL & " WHERE producttype_id = " & IntProductType

                Rst = CommonDataFunctions.GetSQLDatatable(StrSQL)

                If Rst.Rows.Count > 0 Then
                    'Found a product type, try that

                    Select Case IntSet
                        Case 1
                            returnValue = Rst.Rows(0)("Producttype_StageDiagramID")

                        Case 2
                            returnValue = Rst.Rows(0)("Producttype_MFBGroupID")

                        Case 3
                            returnValue = Rst.Rows(0)("Producttype_SLSID")

                        Case 4
                            returnValue = Rst.Rows(0)("Producttype_ChargeGroupID")

                        Case 5
                            returnValue = Rst.Rows(0)("Producttype_EventGroupID")

                        Case 6
                            returnValue = Rst.Rows(0)("Producttype_DTCategory")

                        Case 7
                            returnValue = Rst.Rows(0)("Producttype_CalcSet")

                        Case Else
                            returnValue = 0

                    End Select
                End If

                Rst.Dispose()
            End If
        Else
            'No Product, set to zero and let the calling func handle it
            returnValue = 0
        End If
        Return returnValue
    End Function

    Private Sub Btn_Update_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles Btn_Update.Click
        Try

            Dim Rst As DataTable, StrSQL As String
            Dim StrMsg As String, StrConfirm As String, LngCompanyID As Long, LngContactID As Long, IntStatus As Integer, StrStatus As String, IntOldStatus As Integer

            If Me.Combo_Status.SelectedIndex = -1 Then
                Call MsgBox("Please select the status you want to update to")
                Me.Combo_Status.Focus()
                Exit Sub
            End If


            StrSQL = "SELECT L_CaseStages.CaseStage_Title, Case_StatusID"
            StrSQL = StrSQL & " FROM D_Case LEFT OUTER JOIN"
            StrSQL = StrSQL & " L_CaseStages ON D_Case.Case_StatusID = L_CaseStages.CaseStage_Number"
            StrSQL = StrSQL & " WHERE (D_Case.Case_ID = " & Me.Txt_CaseID.Text & ")"

            Rst = CommonDataFunctions.GetSQLDatatable(StrSQL)

            If Rst.Rows.Count = 0 Then
                Rst.Dispose()
                Call MsgBox("Case number " & Me.Txt_CaseID.Text & " not found!", , "Error")
                Exit Sub
            End If

            StrMsg = "Case status changed from " & Rst.Rows(0)("CaseStage_Title") & " to " & Me.Combo_Status.Text & " using the admin tool"
            IntOldStatus = Rst.Rows(0)("Case_StatusID")

            Rst.Dispose()

            If MsgBox("Are you sure you want to change the status of this case?", vbYesNo, "Confirm update") <> 6 Then Exit Sub

            StrSQL = "Update D_Case set Case_StatusID = " & Me.Combo_Status.SelectedValue & ", case_dtstatus = getdate() where case_id = " & Me.Txt_CaseID.Text

            If CommonDataFunctions.ExecuteSQL(StrSQL) = False Then Exit Sub

            Call Admin.Gblfunc_Add_CaseAudit(Me.Txt_CaseID.Text, 6, CLng(IntOldStatus), Me.Combo_Status.SelectedValue)
            'Call GblFunc_Add_Event(Me.Combo_Status.SelectedValue, StrMsg, 1, Me.Txt_CaseID, 1)
            Call (New TasksEvents).GblFunc_Add_SysEvent("Case Status Changed", "Case number " & Me.Txt_CaseID.Text & vbCrLf & StrMsg)
            Dim tsk As New TasksEvents
            tsk.GblFunc_Add_Event(Me.Combo_Status.Text, StrMsg, 1, Convert.ToDouble(Me.Txt_CaseID.Text), 1)
            Me.Close()
        Catch ex As Exception
            CommonFuncs.ErrorHandler(ex, "frmAdminManageCases-Btn_Update_Click")
        End Try
    End Sub
    

    Private Sub frmAdminManageCases_Loaded(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles Me.Loaded
        GeneralTasks.ChangeTheme(Me)
        GeneralTasks.EnumerateandFill(Me.LogicalChildren)
        'Me.SQL_CaseStatus()
    End Sub
End Class
